import { Toast } from "vant";
import cTitle from "components/title";
export default {
  components: {
    cTitle
  },
  data() {
    return {
      discoverInfo: [],
      isSotreKey: 1, //开启消费券插件
      discoverPage: 1,
      discoverLastPage: 0,
      discoverTap: false,
      // 热门搜索
      hotInfo: [],
      storeInfo: [],
      hotPage: 1,
      hotLastPage: 0,
      hotTap: false,
      keyWord: "",
      searchHistory: []
    };
  },
  activated() {
    if ((this.$route.query.searchPage ?? "") !== "") {
      this.isSotreKey = Number(this.$route.query.searchPage);
    }

    this.storeBaseData();
    try {
      let History = localStorage.getItem("searchHistory") || [];
      console.log(History);
      this.searchHistory = JSON.parse(History);
      console.log(this.searchHistory[0]);
      if (this.searchHistory[0]) {
        this.keyWord = this.searchHistory[0].name;
      }
    } catch (error) {
      console.log(error);
    }
  },
  methods: {
    goSearch() {
      console.log(this.keyWord.length);
      if (this.keyWord.length > 0) {
        console.log(this.isSotreKey);
        if (this.isSotreKey === 3) {
          console.log(this.isSotreKey);
          this.$router.push(this.fun.getUrl("consumptionSearch", {}, { keyword: this.keyWord }));
        }
      } else {
        this.$router.go(-1);
      }
    },
    bindinp(key) {
      console.log(key);
      this.keyWord = key;
    },
    tapKeyWord(key) {
      this.keyWord = key;
      this.$router.push(this.fun.getUrl("consumptionSearch", {}, { keyword: this.keyWord }));
    },
    tapUrl(item) {
      try {
        if(item.web_url) {
          window.location.href = item.web_url;
        }
      } catch (error) {
        console.log(error);
      }
    },
    deleHistory() {
      localStorage.setItem("searchHistory", "");
      this.searchHistory = [];
    },
    colorFin(a) {
      a = a.substring(1);
      a = a.toLowerCase();
      let b = new Array();
      for (let x = 0; x < 3; x++) {
        b[0] = a.substr(x * 2, 2);
        b[3] = "0123456789abcdef";
        b[1] = b[0].substr(0, 1);
        b[2] = b[0].substr(1, 1);
        b[20 + x] = b[3].indexOf(b[1]) * 16 + b[3].indexOf(b[2]);
      }
      return b[20] + "," + b[21] + "," + b[22];
    },
    storeBaseData() {
      $http.post("plugin.coupon-store.frontend.search.searchBaseData", {}, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            let inif = res.keyword;
            for (let i = 0; i < inif.length; i++) {
              inif[i].color = this.colorFin(inif[i].color);
            }
            this.discoverInfo = inif;
            this.storeInfo = res.hot;
            this.discoverLastPage = res.last_page;
            this.discoverTap = true;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    }
  }
};
